﻿@using DCMS.Web.Extensions;
@using DCMS.Web.Framework.UI;
@using DCMS.Web.Framework;
@using DCMS.Core.Domain.Common;
@using DCMS.ViewModel.Models.Terminals;

@model ReceivableListModel

<section id="content_wrapper">

    <!-- Start: Topbar-Dropdown -->
    <div id="topbar-dropmenu">
        @await Html.PartialAsync("_ToolBox")
    </div>
    <!-- End: Topbar-Dropdown -->
    <!-- Start: Topbar -->
    <header id="topbar">
        <div class="topbar-left">
            <ol class="breadcrumb">
                <li class="crumb-active">
                    <a href="#">档案</a>
                </li>
                <li class="crumb-icon">
                    <a href="@Url.RouteUrl("HomePage")">
                        <span class="glyphicon glyphicon-home"></span>
                    </a>
                </li>
                <li class="crumb-link">
                    <a href="#">往来相关</a>
                </li>
                <li class="crumb-trail">应收款初期</li>
            </ol>
        </div>
        <div class="topbar-right">
            <div class="ml15 ib va-m" id="toggle_sidemenu_r">
                <a href="#" class="pl5">
                    <i class="fa fa-sign-in fs22 text-primary"></i>
                    <span class="badge badge-hero badge-danger">3</span>
                </a>
            </div>
        </div>
    </header>
    <!-- End: Topbar -->
    <!-- Begin: Content -->
    <section id="content" class="formSection">

        <div class="special-alerts">
            <div class="alert alert-danger light alert-dismissable" id="alert-demo-2" style="display: none;">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                <i class="fa fa-info pr10"></i>
                <strong>您没有被授权此操作!</strong>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12">

                <form asp-action="List" asp-controller="Receivable" method="get" id="formSearch" novalidate="novalidate">
                    <div class="panel panel-default" id="panel0">
                        <div class="panel-heading">
                            <span class="glyphicon glyphicon-search"></span> 高级搜索
                            <a class="btn btn-default btn-sm pull-right m5 mrn p5 pbn" id="AdvancedSearch"><i class="fa fa-angle-double-right"></i></a>
                        </div>
                        <div class="panel-body p10">
                            <div class="row">
                                <div class="col-sm-2">
                                    <input asp-for="TerminalId" type="hidden" />
                                    <div class="input-group">
                                        <input asp-for="TerminalName" class="form-control" placeholder="客户..." />
                                        <span class="input-group-btn">
                                            <button id="btn_search_customer" type="button" class="btn btn-default">
                                                <span class="glyphicon glyphicon-search"></span>
                                            </button>
                                        </span>
                                    </div>
                                </div>
                                <div class="col-sm-2">
                                    <button id="btn_query" type="submit" class="btn btn-primary btn-block">
                                        <span class="glyphicon glyphicon-search mr5" aria-hidden="true"></span>查询
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>

                </form>

                <div class="panel">
                    <div class="panel-heading">
                        <div class="panel-title">
                            <span class="glyphicon glyphicon-tasks"></span> 应收款列表
                        </div>
                    </div>
                    <div class="panel-body p10">

                        <div id="toolbar" class="btn-group mb10">
                            <a href="@Url.Action("Create","Receivable")" class="btn btn-primary btn-sm mr5">
                                <span class="glyphicon glyphicon-plus mr5"></span>添加
                            </a>
                            <button id="btn_delete" type="button" class="btn btn-danger btn-sm mr5">
                                <span class="glyphicon glyphicon-remove mr5"></span>删除
                            </button>
                            <button id="btn_import" type="button" class="btn btn-primary btn-sm mr5">
                                <span class="glyphicon glyphicon-tasks mr5"></span>导入
                            </button>
                            <button id="btn_Init" type="button" class="btn btn-primary btn-sm mr5">
                                <span class="glyphicon glyphicon-tasks mr5"></span>批量初始化
                            </button>
                        </div>

                        <div class="bootstrap-table">

                            <div class="fixed-table-container">
                                <div class="fixed-table-body">
                                    <table class="table table-striped table-bordered table-hover">
                                        <thead>
                                            <tr class="datagrid-header-row">
                                                <th class="bs-checkbox "><div class="th-inner"><input name="btSelectAll" type="checkbox"></div></th>
                                                <th class="text-center"><div class="th-inner">序号</div></th>
                                                <th class="text-left"><div class="th-inner">客户</div></th>
                                                <th class="text-left"><div class="th-inner">老板名称</div></th>
                                                <th class="text-left"><div class="th-inner">老板手机</div></th>
                                                <th class="text-right"><div class="th-inner">初始欠款</div></th>
                                                <th class="text-left"><div class="th-inner">业务员</div></th>
                                                <th class="text-center"><div class="th-inner">欠款时间</div></th>
                                                <th class="text-center"><div class="th-inner">初始化状态</div></th>
                                                <th class="text-left"><div class="th-inner">备注</div></th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @{
                                                if (Model.Lists == null || Model.Lists.Count == 0)
                                                {
                                                    <tr class="no-records-found"><td colspan="9">没有找到匹配的记录</td></tr>
                                                }
                                                else
                                                {
                                                    foreach (var item in Model.Lists)
                                                    {
                                        <tr>
                                            <td class="bs-checkbox text-center">
                                                <input name="btSelectItem" class="btSelectItem" type="checkbox" value="@item.Id">
                                            </td>
                                            <td class="text-center"><a href="@Url.Action("Edit","Receivable",new { id= item.Id})">@item.Id</a></td>
                                            <td class="text-left">@item.TerminalName</td>
                                            <td class="text-left">@item.BossName</td>
                                            <td class="text-left">@item.BossCall</td>
                                            <td class="text-right">￥ @((item.OweCash).ToString("0.00"))</td>
                                            <td class="text-left">@item.OperationUserName</td>
                                            <td class="text-center">@(item.BalanceDate.ToString("yyyy/MM/dd HH:mm:ss"))</td>
                                            <td class="text-center">
                                                @if (item.Inited == false)
                                                {
                                                <span class="label label-danger">未初始</span>
                                                }
                                                else { 
                                            <span class="label label-success">已初始</span>
                                                }
                                            </td>
                                            <td class="text-left">@item.Remark</td>
                                        </tr>
                                                    }
                                                }
                                            }

                                        </tbody>
                                    </table>
                                </div>
                            </div>

                            @{
                                var pager = Html.Pager(Model.PagingFilteringContext).QueryParam("pagenumber");
                            }

                            @if (!pager.IsEmpty())
                            {
                                <div class="fixed-table-pagination" style="display: block;">
                                    @Html.Raw(pager.ToString())
                                </div>
                            }

                            <div class="clearfix"></div>
                        </div>

                    </div>
                </div>
            </div>
        </div>

    </section>

</section>

<div id="FileForm" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">导入数据</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-md-12">
                        <form asp-action="ImportInitData" asp-controller="StoreDataInit" asp-route-returnurl="@Context.Request.Query["ReturnUrl"]" method="post" class="form-horizontal" id="DataInitForm" enctype="multipart/form-data">
                            <div class="panel">
                                <div class="admin-form p10 pt10">
                                    <div class="tab-content">
                                        <div class="tab-pane active">
                                            <div class="row">
                                                <div class="col-lg-7">
                                                    <div class="section">
                                                        <label class="field prepend-icon append-button file">
                                                            <span class="button btn-primary">选择文件</span>
                                                            <input type="file" class="gui-file" name="file1" id="file1" onchange="document.getElementById('uploader').value = this.value;" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
                                                            <input type="text" class="gui-input" id="uploader" placeholder="请选择文件">
                                                            <label class="field-icon">
                                                                <i class="fa fa-upload"></i>
                                                            </label>
                                                        </label>
                                                    </div>
                                                </div>
                                                <div class="col-lg-2  mr5" style="text-align:center;">
                                                    <a href="javascript:void(0);" id="btn_Rece" class="btn btn-info">
                                                        <span class="glyphicon glyphicon-hand-up mr5"></span>一键导入
                                                    </a>
                                                </div>
                                                <div class="col-lg-2 mr5">
                                                    <a id="btn_downBrand" href="@Url.Action("UploadDownTemplate","StoreDataInit",new { order=13 })" class="btn btn-success">
                                                        <span class="glyphicon glyphicon-download-alt"></span>下载模板
                                                    </a>
                                                </div>
                                            </div>


                                        </div>


                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <div class="alert alert-info alert-dismissable" style="padding:0;">
                    <i class="fa fa-warning pr10"></i>
                    <strong style="margin-right:40px;">温馨提示：请先下载模板，按照模板填写数据进行导入！</strong>
                    <button type="button" class="btn btn-default btn-sm" data-dismiss="modal"><i class="fa fa-undo mr5"></i>取消</button>
                </div>
            </div>
        </div>
    </div>
</div>

@{
    await Html.RenderPartialAsync("_ModalForm", new ViewDataDictionary(ViewData) { { "formId", "TerminalSelectForm" }, { "modalWindow", "TerminalSelectModalWindow" }, { "windowWidth", "1000px" }, { "showSave", "true" } });

}


@section CurPageScripts
{
  

    <script type="text/javascript">

        jQuery(document).ready(function () {

            "use strict";

            // Init Theme Core
            Core.init();

            // Init Demo JS
            Demo.init();

            //权限提示
            if ($(this).getUrlQueryString("Authorize") == "noAuthorize") {
                $('#alert-demo-2').fadeToggle();
                setTimeout(function () {
                    $('#alert-demo-2').fadeToggle();
                }, 3000);
            }

            //全选
            $("input[name='btSelectAll']").click(function () {
                var selectStatus = $(this).is(':checked')
                if (selectStatus) {
                    $("input[name='btSelectItem']").each(function (i, n) {
                        n.checked = true;
                    });
                } else {
                    $("input[name='btSelectItem']").each(function (i, n) {
                        n.checked = false;
                    });
                }
            });

            //高级搜索
            $("#panel0 #AdvancedSearch").click(function () {
                var ico = $(this).find(".fa");
                if (ico.hasClass('fa-angle-double-right'))
                    ico.removeClass("fa-angle-double-right").addClass("fa-angle-double-down");
                else
                    ico.removeClass("fa-angle-double-down").addClass("fa-angle-double-right");
                $("#panel0").find(".panel-body").toggleClass("hidden fadeIn");
            });

            //客户搜索
            $(document).on('click', "#btn_search_customer", function () {
                var actionUrl = "/Terminal/AsyncSearchSelectPopup";
                $(this).showModalV2($("#TerminalSelectModalWindow"), actionUrl, { ids: "" }, "选择终端", $("#TerminalSelectForm"), function (rows) { });
            });

            window.operateEventsPopTerminal = {
                'click .rowSelect': function (e, value, row, index) {
                    try {
                        $("#TerminalId").val(row.Id);
                        $("#TerminalName").val(row.Name);
                        $('#TerminalSelectModalWindow').modal('hide');
                    }
                    catch (err) {
                        console.log(err);
                        $(this).showToastr(err);
                        return;
                    }
                }
            };
            $("#btn_Init").click(function () {
                var selectData = "";
                //获取用户选择的单据
                var arrselections = $("input[name='btSelectItem']:checked").each(function (i, j) {//把所有被选中的复选框的值存入数组
                    selectData += (selectData != "" ? "," : "") + $(this).val();
                });

                if (selectData == "") {
                    $(this).showToastr("没有选择数据");
                    return false;
                }
                var $tb = $("#datatable");
                WinMsg.confirm({ message: "初始化选择的单据吗？" }).on(function (e) {
                    if (!e) {
                        return;
                    }
                    $.ajax({
                        type: "get",
                        url: "/Receivable/Init",
                        data: {
                            selectData: selectData
                        },
                        contentType: "application/json",
                        dataType: "json",
                        success: function (result) {
                            if (result.Success) {

                                toastr.success(result.Message);
                                window.location.href = "/Receivable/List";
                            }
                            else {
                                $(this).showToastr(result.Message);
                            }
                        },
                        error: function () {
                            $(this).showToastr('网络错误，请重新保存！');
                        }
                    });
                });
            });
            //删除
            $("#btn_delete").click(function () {

                var selectData = "";
                //获取用户选择的单据
                var arrselections = $("input[name='btSelectItem']:checked").each(function (i, j) {//把所有被选中的复选框的值存入数组
                    selectData += (selectData != "" ? "," : "") + $(this).val();
                });

                if (selectData == "") {
                    $(this).showToastr("没有选择数据");
                    return false;
                }

                var $tb = $("#datatable");
                WinMsg.confirm({ message: "删除选择的单据吗？" }).on(function (e) {
                    if (!e) {
                        return;
                    }
                    $.ajax({
                        type: "get",
                        url: "/Receivable/Delete",
                        data: {
                            selectData: selectData
                        },
                        contentType: "application/json",
                        dataType: "json",
                        success: function (result) {
                            if (result.Success) {

                                toastr.success(result.Message);
                                window.location.href = "/Receivable/List";
                            }
                            else {
                                $(this).showToastr(result.Message);
                            }
                        },
                        error: function () {
                            $(this).showToastr('网络错误，请重新保存！');
                        }
                    });
                });
            });

            //导入商品
            $("#btn_import").click(function () {
                $('#FileForm').modal('show');
            });

            $("#btn_Rece").click(function () {
                if ($("#uploader").val() == "") {
                    $(this).showToastr("请选择文件!");
                    return;
                }
                var fileUpload = $("#file1").get(0);//获得第一个files的名称和值
                if (fileUpload.files.length > 0) {
                    var files = fileUpload.files;//获取文件信息
                    var data = new FormData();//通过FormData构造函数创建一个空对象
                    data.append(files[0].name, files[0]);//通过append方法追加数据
                    $.ajax({
                        type: "post",
                        url: "/StoreDataInit/ImportInitData?order=13",
                        contentType: false,
                        processData: false,
                        data: data,
                        success: function (data) {
                            if (data.Success) {
                                toastr.success(data.Message);
                                $('#FileForm').modal('hide');
                            }
                            else {
                                $(this).showToastr(data.Message);
                            }
                        },
                        error: function () {
                            $(this).showToastr("上传文件出现错误！");
                        }
                    });
                }
                else {
                    $(this).showToastr("请选择文件!");
                }
            });
        });

    </script>
}